// Copyright (c) 2009 Mike Haight, Andy Kipp
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#include "stdafx.h"

Map::Map(void)
{
}

int Map::Rows::get(void)
{
    return mRows;
}

int Map::Columns::get(void)
{
    return mColumns;
}

Tile^ Map::GetSquare(int argColumn, int argRow)
{
    if (argColumn < 0 || argColumn >= mColumns)
        return nullptr;

    if (argRow < 0 || argRow >= mRows)
        return nullptr;

    int index = (argRow * mRows) + argColumn;
    return mTiles[index];
}

Map^ Map::Load(MapDescription^ argDesc)
{
    if (argDesc == nullptr)
        return nullptr;

    smLogger->Info("Loading Map: " + argDesc->Data + ".");

    // Create Map
    Map^ map = gcnew Map();

    // Get Map Properties
    map->mName = argDesc->Name;
    map->mRows = argDesc->Rows;
    map->mColumns = argDesc->Columns;

    // Load Map Texture
    {
        map->mTexture = ResourceManager::GetTexture(argDesc->Texture);
    }

    // Load Map Data
    {
        try
        {
            // Get Square Height
            // TODO: This should be a const somewhere
            int height = 32;
            int width = 32;

            // Create FileStream
            FileStream^ fs = gcnew FileStream(argDesc->Data, FileMode::Open, FileAccess::Read);

            long index;
            for (index = 0; index < fs->Length; index++)
            {
                // Read Byte
                int b = fs->ReadByte();
                if (b < 0)
                    break;

                int y = (index / map->mRows);
                int x = (index - (y *  map->mColumns));

                // Load Tile
                Tile^ tile = Tile::Load(ResourceManager::GetTile(map->mName, b), map->mTexture);

                // Set Tile Size and Location
                tile->Location =          Location((float)x * width, (float)y * height);
                tile->Size =              Size((float)width, (float)height);

                // Update Tile to move Sprite
                tile->Update();

                // Add Tile to Layout List
                map->mTiles.Insert(index, tile);
            }
        }
        catch (Exception^ ex) 
        {
            smLogger->ErrorException("Error Reading Map File: " + argDesc->Data + ".", ex);
        }
    }

    smLogger->Info("Loaded Map: " + argDesc->Data + ".");

    // Return Map
    return map;

}